-- shield 护盾机制
-- 先获得的护盾先 去抗伤害

ac.buff("护盾") {

    physics = true,
    keep = true,

    cover_max = 99,
    cover_type = 1,

    shield = 0, -- 护盾值
    time = -1,

    on_add = function(self)
        local unit = self.owner 
        unit:add_shield(self.shield)
        self.trigger = unit:on "单位-伤害护盾计算" (function (_, _, source, damage)
            local value = damage:get_current_damage()
            if value > 0 then 
                local cost = math.min(self.shield, value)
                self.shield = self.shield - cost 
                self.owner:add_shield(- cost)
                value = value - cost
                damage:set_current_damage(value)
                if self.shield <= 0 then 
                    -- 护盾破碎的时候触发
                    self:notify("on_finish")
                    self:remove()
                end
                if value <= 0 then 
                    return true
                end
            end 
        end)
    end,

    on_remove = function(self)
        local unit = self.owner 
        unit:add_shield(-self.shield)
        if self.trigger then 
            self.trigger:remove()
        end 
    end,

    on_cover = function(self, new)
        return true 
    end,
}
